package org.celstec.mobilegame.locatory;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.celstec.mobilegame.gamaray.Feature;
import org.celstec.mobilegame.gamaray.FeatureImg;
import org.celstec.mobilegame.server.PMF;
import org.celstec.mobilegame.server.SubmitGameServiceImpl;
import org.celstec.mobilegame.util.WorldObject;
import org.jdom.Element;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class CardDropOffZone implements WorldObject {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	public Key id;
	
	
	@Persistent
	public double lat;
	@Persistent
	public double lon;
	
	@Persistent(defaultFetchGroup="true")
	public Key card;
	@Persistent
	public String cardId;
	@Persistent
	public int points;
	
	@Persistent(defaultFetchGroup="true")
	public Key locatory;
	
	public static CardDropOffZone getObject (Key k){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			return (CardDropOffZone) pm.getObjectById(CardDropOffZone.class, k);
		} finally {			
			pm.close();

		}
	}
	
	public CardDropOffZone(){
//		key = KeyFactory.createKey(CardDropOffZone.class.getSimpleName(), ""+System.currentTimeMillis());
	}
	
	public CardDropOffZone(Element zone){
		this();
//		id += idGenerator++;
		if (zone.getAttributeValue("lat")!= null) lat = Double.parseDouble(zone.getAttributeValue("lat"));
		if (zone.getAttributeValue("lon")!= null) lon = Double.parseDouble(zone.getAttributeValue("lon"));
		if (zone.getAttributeValue("points")!= null) points = Integer.parseInt(zone.getAttributeValue("points"));
		if (zone.getAttributeValue("cardId")!= null) cardId = zone.getAttributeValue("cardId");
	}
	

	public double getLat() {
		return lat;
	}

	public double getLon() {
		return lon;
	}

	public Feature getFeature(){
		return new FeatureImg("dropzone_"+id, "dropOffZone", lat, lon, "CC", null);
	}
	
	public String toString(){
		return "CardDropOffZone: "+lat+" "+lon+" "+Card.getObject(card).cardId+" "+points+" ( "+Card.getObject(card).cardId
		+" "+Card.getObject(card).cardImageType+" "+Card.getObject(card).cardImage+")";
	}

}
